Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Questions  >  What does the following function do?int fun(i... Start Learning for Free
What does the following function do?
int fun(int x, int y)
{
    if (y == 0)   return 0;
    return (x + fun(x, y-1));
}
  • a)
    x + y
  • b)
    x + x*y
  • c)
    x*y
  • d)
    xy
Correct answer is option 'C'. Can you explain this answer?
Verified Answer
What does the following function do?int fun(int x, int y){ if (y == 0...
The function adds x to itself y times which is x*y.
View all questions of this test
Most Upvoted Answer
What does the following function do?int fun(int x, int y){ if (y == 0...
Explanation:
The given function is a recursive function that calculates the product of two numbers, x and y. Let's break down the code and understand how it works.

1. Base Case:
The function starts with a base case that checks if the value of y is equal to 0. If it is, the function returns 0. This is the stopping condition for the recursion.

2. Recursive Case:
If the base case is not satisfied, the function calls itself recursively with the parameters x and y-1. In each recursive call, the value of y is reduced by 1 until it reaches 0.

3. Return Value:
In each recursive call, the function multiplies the value of x with the result of the recursive call (i.e., fun(x, y-1)). This is done recursively until the base case is reached.

4. Example:
Let's take an example to understand how the function works. Suppose we have x = 3 and y = 4.

- The first call to the function is fun(3, 4).
- Since y is not 0, the function goes to the recursive case and calls fun(3, 3).
- Again, y is not 0, so the function calls fun(3, 2).
- This process continues until y becomes 0. At that point, the base case is satisfied, and the function returns 0.
- Now, in the previous recursive call, the value of y is 1. So, the function returns x * fun(x, y-1), i.e., 3 * 0 = 0.
- Similarly, in the initial call, the function returns x * fun(x, y-1), i.e., 3 * 0 = 0.

5. Final Result:
In the end, the function returns the product of x and y, which is 3 * 4 = 12.

Therefore, the correct answer is option 'C' (x * y), as the function calculates the product of the two input numbers using recursion.
Free Test
Community Answer
What does the following function do?int fun(int x, int y){ if (y == 0...
Function Explanation:
The given function is a recursive function that takes two integer parameters, x and y. It calculates the product of x and y using recursion.

Function Algorithm:
1. Check if the value of y is equal to 0.
- If true, return 0.
- This is the base case of the recursion.
2. If y is not equal to 0, recursively call the function with the arguments x and y-1.
3. Multiply the value of x with the result of the recursive call.
4. Return the product.

Function Execution:
Let's understand the execution of the function with an example:
Suppose we call the function with the values x = 3 and y = 4.

1. Initial call: fun(3, 4)
2. Recursive call 1: fun(3, 3)
3. Recursive call 2: fun(3, 2)
4. Recursive call 3: fun(3, 1)
5. Recursive call 4: fun(3, 0)

At this point, the base case is reached because y = 0.

6. Return 0
7. Multiply x (which is 3 in this case) with the result of the recursive call (which is 0).
8. Return the product: 3 * 0 = 0

So, the final result is 0.

Function Output:
The function returns the product of x and y, which is x * y.

Therefore, the correct answer is option C: x * y.
Explore Courses for Computer Science Engineering (CSE) exam
Question Description
What does the following function do?int fun(int x, int y){ if (y == 0) return 0; return (x + fun(x, y-1));}a)x + yb)x + x*yc)x*yd)xyCorrect answer is option 'C'. Can you explain this answer? for Computer Science Engineering (CSE) 2025 is part of Computer Science Engineering (CSE) preparation. The Question and answers have been prepared according to the Computer Science Engineering (CSE) exam syllabus. Information about What does the following function do?int fun(int x, int y){ if (y == 0) return 0; return (x + fun(x, y-1));}a)x + yb)x + x*yc)x*yd)xyCorrect answer is option 'C'. Can you explain this answer? covers all topics & solutions for Computer Science Engineering (CSE) 2025 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for What does the following function do?int fun(int x, int y){ if (y == 0) return 0; return (x + fun(x, y-1));}a)x + yb)x + x*yc)x*yd)xyCorrect answer is option 'C'. Can you explain this answer?.
Solutions for What does the following function do?int fun(int x, int y){ if (y == 0) return 0; return (x + fun(x, y-1));}a)x + yb)x + x*yc)x*yd)xyCorrect answer is option 'C'. Can you explain this answer? in English & in Hindi are available as part of our courses for Computer Science Engineering (CSE). Download more important topics, notes, lectures and mock test series for Computer Science Engineering (CSE) Exam by signing up for free.
Here you can find the meaning of What does the following function do?int fun(int x, int y){ if (y == 0) return 0; return (x + fun(x, y-1));}a)x + yb)x + x*yc)x*yd)xyCorrect answer is option 'C'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of What does the following function do?int fun(int x, int y){ if (y == 0) return 0; return (x + fun(x, y-1));}a)x + yb)x + x*yc)x*yd)xyCorrect answer is option 'C'. Can you explain this answer?, a detailed solution for What does the following function do?int fun(int x, int y){ if (y == 0) return 0; return (x + fun(x, y-1));}a)x + yb)x + x*yc)x*yd)xyCorrect answer is option 'C'. Can you explain this answer? has been provided alongside types of What does the following function do?int fun(int x, int y){ if (y == 0) return 0; return (x + fun(x, y-1));}a)x + yb)x + x*yc)x*yd)xyCorrect answer is option 'C'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice What does the following function do?int fun(int x, int y){ if (y == 0) return 0; return (x + fun(x, y-1));}a)x + yb)x + x*yc)x*yd)xyCorrect answer is option 'C'. Can you explain this answer? tests, examples and also practice Computer Science Engineering (CSE) tests.
Explore Courses for Computer Science Engineering (CSE) exam
Signup to solve all Doubts
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev